package com.gongpingjialoan.widget;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.gongpingjialoan.R;
import com.gongpingjialoan.activity.car.SecondDetailFragment;
import com.gongpingjialoan.activity.vip.VIPDetailActivity;
import com.gongpingjialoan.api.Piont;
import com.gongpingjialoan.data.UserManager;
import com.gongpingjialoan.global.GPJApplication;
import com.gongpingjialoan.manage.PointRecordManage;
import com.gongpingjialoan.utility.Utils;

/**
 * Created by Administrator on 2016/5/23.
 */
public class CutPricePopup extends PopupWindow implements View.OnClickListener {


    private Context mContext;

    private SecondDetailFragment mFragment;

    private View mView, main, main1, evalView;

    private TextView dealer_price, evalTextView, eval_price, callTextView;

    private EditText phoneEditText, priceEditText;

    private ImageView close;

    private Button mButton;

    public boolean showMsg = false;

    View vipdesV;

    public CutPricePopup(Fragment fragment, boolean showMsg) {
        super(fragment.getActivity());
        this.showMsg = showMsg;
        mFragment = (SecondDetailFragment) fragment;
        this.mContext = fragment.getActivity();
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        mView = inflater.inflate(R.layout.cut_price_layout, null);

        this.setContentView(mView);
        evalView = mView.findViewById(R.id.eval_main);
        main = mView.findViewById(R.id.main);
        main1 = mView.findViewById(R.id.main1);
        close = (ImageView) mView.findViewById(R.id.close);
        mButton = (Button) mView.findViewById(R.id.sure_button);
        phoneEditText = (EditText) mView.findViewById(R.id.phone);
        priceEditText = (EditText) mView.findViewById(R.id.price);
        dealer_price = (TextView) mView.findViewById(R.id.dealer_price);
        evalTextView = (TextView) mView.findViewById(R.id.eval);
        eval_price = (TextView) mView.findViewById(R.id.eval_price);
        callTextView = (TextView) mView.findViewById(R.id.call);
        if (Utils.isLing(mFragment.buy_eval_price)) {
            evalView.setVisibility(View.GONE);
        } else {
            evalView.setVisibility(View.VISIBLE);
            eval_price.setText("参考价格" + mFragment.buy_eval_price + "万");
        }
        evalTextView.setOnClickListener(this);
        callTextView.setOnClickListener(this);
        dealer_price.setText(mFragment.mPrice + "万");
        if (GPJApplication.getInstance().isLogin()) {
            UserManager mUserManager = new UserManager(fragment.getActivity());
            mUserManager = mUserManager.LoadUserInfo();
            phoneEditText.setText(mUserManager.getPhone());
        }

        vipdesV = mView.findViewById(R.id.vip_des);
        vipdesV.setVisibility(showMsg ? View.VISIBLE : View.GONE);
        vipdesV.setOnClickListener(this);

        close.setOnClickListener(this);
        mButton.setOnClickListener(this);
        main.setOnClickListener(this);
        main1.setOnClickListener(this);
        this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable cd = new ColorDrawable(0x000000);
        this.setBackgroundDrawable(cd);
    }

    @Override
    public void onClick(View v) {
        if (v == close || v == main) {
            mDismiss();
        } else if (v == mButton) {
            PointRecordManage.getInstance().addPoint(mContext, Piont.SourceCutPriceCommit);
            if (TextUtils.isEmpty(priceEditText.getText().toString())) {
                PointRecordManage.getInstance().addPoint(mContext, Piont.SourceCutPriceAlert);
                Toast.makeText(mContext, "请输入预期价格", Toast.LENGTH_SHORT).show();
                return;
            }
            String num = phoneEditText.getText().toString();
            if (num.length() < 11) {
                PointRecordManage.getInstance().addPoint(mContext, Piont.SourceCutPriceAlert);
                Toast.makeText(mContext, "请输入正确的手机号码", Toast.LENGTH_SHORT).show();
                return;
            }
            mDismiss();
            mFragment.cutPrice(num, priceEditText.getText().toString());
        } else if (v == evalTextView) {
            mDismiss();
            mFragment.assessData();
        } else if (v == callTextView) {
            Utils.callKefu(mContext);
        } else if (v == vipdesV) {
            Intent it = new Intent(mContext, VIPDetailActivity.class);
            it.putExtra("from", "detail_bargain");
            mContext.startActivity(it);

        }

    }

    private void mDismiss(){
        PointRecordManage.getInstance().addPoint(mContext, Piont.SourceCutPriceClose);
        dismiss();
    }


}
